﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Shell.DomainDot;
using Framework.Poco;
using Framework.Domain;

namespace Framework.Mvc.Controllers
{
    public class AgencyVideoController : Controller
    {
        private IDomainDotFactory factory;

        public AgencyVideoController()
        {
            factory = DomainDotFactory.Factory;
        }

        /// <summary>
        /// 创建经销商的视频信息
        /// </summary>
        public ActionResult Create(Agency currentAgency, Video instance)
        {
            if (currentAgency != null)
            {
                var context = factory.Create<AgencyVideoContext>(currentAgency.Id);
                context.Create(instance);
                return this.RedirectToAction("List");
            }
            return this.RedirectToAction("LogOn", "Agency");
        }

        /// <summary>
        /// 获取删除信息
        /// </summary>
        [HttpGet]
        public ActionResult Delete(Agency currentAgency, Guid identity)
        {
            if (currentAgency != null)
            {
                var context = factory.Create<AgencyVideoContext>(currentAgency.Id);
                var model = new AgencyVideoDeleteViewModel();
                model.Instance = context.GetVideo(identity);
                return View(model);
            }
            return this.RedirectToAction("LogOn", "Agency");
        }

        /// <summary>
        /// 提交删除信息
        /// </summary>
        [HttpPost]
        public ActionResult Delete(Agency currentAgency, AgencyVideoDeleteViewModel model)
        {
            if (currentAgency != null)
            {
                var context = factory.Create<AgencyVideoContext>(currentAgency.Id);
                context.Delete(model.Instance.Id);
                return this.RedirectToAction("List");
            }
            return this.RedirectToAction("LogOn", "Agency");
        }

        /// <summary>
        /// 获取修改视频信息
        /// </summary>
        [HttpGet]
        public ActionResult Edit(Agency currentAgency, Guid identity)
        {
            if (currentAgency != null)
            {
                var context = factory.Create<AgencyVideoContext>(currentAgency.Id);
                var model = new AgencyVideoEditViewModel();
                model.Instance = context.GetVideo(identity);
                return View(model);
            }
            return this.RedirectToAction("LogOn", "Agency");
        }

        /// <summary>
        /// 提交修改视频信息
        /// </summary>
        [HttpPost]
        public ActionResult Edit(Agency currentAgency, AgencyVideoDeleteViewModel model)
        {
            if (currentAgency != null)
            {
                var context = factory.Create<AgencyVideoContext>(currentAgency.Id);
                context.Edit(model.Instance);
                return this.RedirectToAction("List");
            }
            return this.RedirectToAction("LogOn", "Agency");
        }

        /// <summary>
        /// 视频列表
        /// </summary>
        public ActionResult List(Agency currentAgency, int currentPage, int pageSize)
        {
            if (currentAgency != null)
            {
                var context = factory.Create<AgencyVideoContext>(currentAgency.Id);
                var list = context.GetAllAgencyVideos();
                list = list.OrderBy(v => v.Created).Skip((currentPage - 1) * pageSize).Take(pageSize);
                var total = list.Count();
                var model = new AgencyVideoListViewModel(list, currentPage, pageSize, total);
                return View(model);
            }
            return this.RedirectToAction("LogOn", "Agency");
        }

        /// <summary>
        /// 视频详情
        /// </summary>
        public ActionResult Detail(Guid identity)
        {
            var model = new AgencyVideoDetailViewModel();
            var context = factory.Create<AgencyVideoContext>();
            model.Instance = context.GetVideo(identity);
            return View(model);
        }
    }
}